package backend.model.worklog;

import backend.model.admin.*;
import backend.model.base.DBModel;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import javax.persistence.*;
import java.util.Date;

@NamedQueries({
        @NamedQuery(name = "loadWorkLogForDateAndEmployee", query = "from WorkLog where employee.id = :employeeId and date = :date"),
        @NamedQuery(name = "loadWorkLogForTimeInterval", query = "from WorkLog where date >= :startDate and date <= :endDate"),
        @NamedQuery(name = "loadWorkLogForTimeIntervalAndEmployee", query = "from WorkLog where employee.id = :employeeId and date >= :startDate and date <= :endDate"),
        @NamedQuery(name = "loadWorkLogForTimeIntervalAndTeam", query = "from WorkLog where team.id = :teamId and date >= :startDate and date <= :endDate"),
        @NamedQuery(name = "loadWorkLogForDateAndForemanList", query = "from WorkLog where hours > 0 and date = :date and foreman in (:foremenList)"),
        @NamedQuery(name = "loadWorkLogOfCommutersForTimeInterval", query = "from WorkLog where date >= :startDate and date <= :endDate and isCommuter = :isCommuter")
})

@Entity
@Table(name = "work_log")
public class WorkLog extends DBModel {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "date")
    private Date date;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @ManyToOne
    @JoinColumn(name = "occupation_id")
    private Occupation occupation;

    @ManyToOne
    @JoinColumn(name = "shift_id")
    private Shift shift;

    @ManyToOne
    @JoinColumn(name = "foreman_id")
    private Foreman foreman;

    @ManyToOne
    @JoinColumn(name = "team_id")
    private Team team;

    @ManyToOne
    @JoinColumn(name = "project_id")
    private Project project;

    @ManyToOne
    @JoinColumn(name = "sfi_id")
    private Sfi sfi;

    @ManyToOne
    @JoinColumn(name = "workplace_id")
    private Workplace workplace;

    @ManyToOne
    @JoinColumn(name = "work_detail_id")
    private WorkDetail workDetail;

    @Column(name = "hours")
    private int hours;

    @Column(name = "is_commuter")
    private boolean isCommuter;

    @Override
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String getIdentificationCard() {
        return getEmployee().getIdentificationCard();
    }

    public Occupation getOccupation() {
        return occupation;
    }

    public void setOccupation(Occupation occupation) {
        this.occupation = occupation;
    }

    public Shift getShift() {
        return shift;
    }

    public void setShift(Shift shift) {
        this.shift = shift;
    }

    public Foreman getForeman() {
        return foreman;
    }

    public void setForeman(Foreman foreman) {
        this.foreman = foreman;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Sfi getSfi() {
        return sfi;
    }

    public void setSfi(Sfi sfi) {
        this.sfi = sfi;
    }

    public Activity getActivity() {
        return sfi != null ? sfi.getActivity() : null;
    }

    public Department getDepartment() {
        return sfi !=null ? sfi.getDepartment() : null;
    }

    public Workplace getWorkplace() {
        return workplace;
    }

    public void setWorkplace(Workplace workplace) {
        this.workplace = workplace;
    }

    public WorkDetail getWorkDetail() {
        return workDetail;
    }

    public void setWorkDetail(WorkDetail workDetail) {
        this.workDetail = workDetail;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public boolean isCommuter() {
        return isCommuter;
    }

    public void setIsCommuter(boolean commuter) {
        isCommuter = commuter;
    }

    @Override
    public boolean getIsActive() {
        return true;
    }

    @Override
    public String getDisplayLabel() {
        return "Booked Day: " + getDate() + " ("+ getHours() + ")";
    }

    @Override
    public int compareTo(Object o) {
        int ret;
        if (!(o instanceof WorkLog)) {
            ret = -1;
        } else {
            WorkLog otherWorkLog = (WorkLog) o;
            ret = getDate().compareTo(otherWorkLog.getDate());
            if (ret == 0) {
                ret = getEmployee().getDisplayLabel().compareTo(otherWorkLog.getEmployee().getDisplayLabel());
            }
        }
        return ret;
    }
}
